package t20220305;

import java.util.Formatter;

/**
 * @Author: DengJia
 * @Date: 2022/3/5 14:35
 * @Description:
 */

public class Receipt {
    private double total = 0;
    private static final Formatter F = new Formatter(System.out);
    private final int width;

    public Receipt(int width) {
        this.width = width;
    }

    public void printTitle() {
        F.format("%-" + 4 * width + "s %" + width + "s %" + 2 * width + "s\n", "Item", "Qty", "Price");
        F.format("%-" + 4 * width + "s %" + width + "s %" + 2 * width + "s\n", "----", "---", "-----");
    }

    public void print(String name, int qty, double price) {
        F.format("%-" + 4 * width + "." + 4 * width + "s %" + width + "d %" + 2 * width + ".2f\n", name, qty, price);
        total += price;
    }

    public void printTotal() {
        F.format("%-" + 4 * width + "s %" + width + "s %" + 2 * width + ".2f\n", "Tax", "", total * 0.07);
        F.format("%-" + 4 * width + "s %" + width + "s %" + 2 * width + "s\n", "", "", "-----");
        F.format("%-" + 4 * width + "s %" + width + "s %" + 2 * width + ".2f\n", "Total", "", total * 1.07);

    }

    public static void main(String[] args) {
        Receipt receipt = new Receipt(7);
        receipt.printTitle();
        receipt.print("Jack's Magic Beans", 4, 4.25);
        receipt.print("Princess Peas", 3, 5.1);
        receipt.print("Three Bears Porridge", 1, 14.29);
        receipt.printTotal();
    }
}
